/**
 @Name：layuiAdmin（iframe版） 系统设置分组
 @Author：chichu
 @Site：http://www.cltphp.com/pro.html
 @License：https://www.cltphp.com/detail-140.html
 */

layui.define(['admin', 'table', 'util'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element, form = layui.form,util = layui.util,gid = $('#gid').val();
    //全部数据
    tableIn = table.render({
        elem: '#list',
        url: '/admin/systemField/index',
        where:{gid:gid},
        toolbar: '#toolbar',
        method: 'post',
        page:true,
        cols: [[
            {checkbox: true, fixed: true},
            {field: 'id', title: '编号', width: 80, fixed: true, sort: true},
            {field: 'field', title: '字段名称', width: 150,edit:'text',sort: true},
            {field: 'name', title: '字段别名', width: 150,edit:'text'},
            {field: 'type_name', title: '字段类型', width: 100,edit:'text'},
            {field: 'sort', align: 'center', title: '排序', width: 80, sort: true,edit:'text'},
            {field: 'required', align: 'center',title: '是否必填', width: 120,toolbar: '#requireds',sort: true},
            {field: 'status', align: 'center',title: '状态', width: 120,toolbar: '#statuss',sort: true},
            {width: 200, align: 'center', toolbar: '#action'}
        ]]
    });
    //监听排序事件
    table.on('sort(list)', function(obj){
        table.reload('list', {
            initSort: obj
            ,where: {sort_by: obj.field,sort_order: obj.type}
        });
    });
    //改变字段状态
    form.on('switch()', function(obj){
        var id = this.value;
        var field = this.id;
        console.log(field);
        var value = obj.elem.checked===true?1:0;
        if(field=='status'){
            var data = {id:id,status:value}
        }else{
            var data = {id:id,required:value}
        }
        admin.req({
            url: '/admin/systemField/status'
            , data: data
            , type:'post'
            , success: function (res) {
                if(res.code>0){
                    if(obj.elem.checked==true){
                        $(obj.elem).prop('checked',false);
                    }else{
                        $(obj.elem).prop('checked',true);
                    }
                    form.render();
                }else{
                    layer.msg(res.msg,{time:1000,icon:1});
                }
            }
        });
    });
    table.on('edit(list)', function(obj){
        var value = obj.value //得到修改后的值
            ,field = obj.field; //得到字段
        admin.req({
            url: '/admin/systemField/cf?id='+obj.data.id+'&'+field+'='+value
            , success: function (res) {
                if(res.code==0){
                    layer.msg(res.msg,{time:1000,icon:1});
                }
                tableIn.reload();
            }
        });
    });
    //监听工具条
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        var id = data.id;
        if (obj.event === 'del') {
            layer.confirm('您确定要删除该字段吗？', function () {
                admin.req({
                    url: '/admin/systemField/del/'
                    , type: 'post'
                    , data: {'id': id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        }else if (obj.event === 'edit') {
            //分组编辑
            var index = layer.open({
                type: 2,
                content: '/admin/systemField/edit/id/' + id,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }
    });
    //编辑提交
    form.on('submit(edit-btn)', function (data) {
        admin.req({
            url: '/admin/systemField/editSave/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });

    //头工具栏事件
    table.on('toolbar(list)', function(obj){
        var gid = $('#gid');
        switch(obj.event){
            case 'add':
                var index = layer.open({
                    type: 2,
                    content: '/admin/systemField/add',
                    area: ['300px', '300px'],
                    maxmin: true
                });
                layer.full(index);
                break;
            case 'direct':
                var checkStatus = table.checkStatus('list'),data = checkStatus.data; //获得选中的数据
                if(data.length === 0) return layer.msg('未选中行',{icon:2});
                var ids = [];
                $.each(data, function(i,val){
                    ids[i] = val['id'];
                });
                layer.confirm('确定删除所选友链吗？', function(){
                    admin.req({
                        url: '/admin/systemField/delAll/'
                        , type: 'post'
                        , data: {ids:ids}
                        , success: function (res) {
                            layer.closeAll('dialog');
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
                break;
        }
    });
    //添加提交
    form.on('submit(add-btn)', function(data){
        admin.req({
            url: '/admin/systemField/addSave/'
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });
    //搜索
    var active = {
        reload: function () {
            var key = $('#key').val();
            //执行重载
            table.reload('list', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {key: key}
            });
        }
    };
    $('.table-head-btn .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    exports('system_field', {});
});